package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.migranttasksign.entity.MigrantTaskSign;
import com.jdjw.modules.migranttasksign.mapper.MigrantTaskSignMapper;
import com.jdjw.modules.thirdpost.param.DwdLswgJiandeParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.modules.thirdpost.param.WgEnum;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 劳务用工基础表 推送数据
 */
@Service
@Slf4j
public class LaborEmploymentServiceImpl extends ServiceImpl<MigrantTaskSignMapper, MigrantTaskSign>
        implements IService<MigrantTaskSign> {

    @Autowired
    static MigrantTaskSignMapper migrantTaskSignMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        LaborEmploymentServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setMigrantTaskSignMapper(MigrantTaskSignMapper migrantTaskSignMapper) {
        LaborEmploymentServiceImpl.migrantTaskSignMapper = migrantTaskSignMapper;
    }

    public static void doPostLaborEmployment(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 13;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 13;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 13;
        }
        try {
            //查询
            QueryWrapper entityWrapper = new QueryWrapper();
            if (StringUtils.isNotBlank(thirdPostInParm.getStartTime())) {
                entityWrapper.ge("create_date", thirdPostInParm.getStartTime());
            } else if (StringUtils.isNotBlank(thirdPostInParm.getEndTime())) {
                entityWrapper.le("create_date", thirdPostInParm.getEndTime());
            }
            List<MigrantTaskSign> all = migrantTaskSignMapper.selectList(entityWrapper);
            if (all == null || all.size() < 1) {
                log.info("劳务用工基础表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<MigrantTaskSign>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<MigrantTaskSign> obj : fgList) {
                    sendData((List<MigrantTaskSign>) obj, url);
                }
            } else {
                sendData(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendData(List<MigrantTaskSign> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (MigrantTaskSign a : all) {
            //组装数据
            DwdLswgJiandeParam param = new DwdLswgJiandeParam();
            param.setId(a.getId());
            param.setBiz_id(a.getId());
            param.setCwh(a.getDeptName());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getDeptId(), 16));
            param.setWgbm(a.getMigrantCode());
            param.setWgjsrq(a.getMigrantStartDate());
            param.setWgksrq(a.getMigrantEndDate());
            param.setWglx(WgEnum.getCodeByMsg(a.getMigrantType()));
            param.setWgsx(a.getMigrantItem());
            param.setDel_flag(Integer.parseInt(a.getDeleted()));


            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("劳务用工基础表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostLaborEmployment", "劳务用工基础表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("劳务用工基础表推送失败", result);
            }
        } else {
            log.info("劳务用工基础表推送失败", "第三方返回空");
        }
    }
}
